package com.roger.myHome.controller;

import com.roger.myHome.util.SystemUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;


@Controller
public class IndexController {

    private static List<String> PAGE_LIST = new ArrayList<>();

    static {
        PAGE_LIST = SystemUtil.getMenuUrlList();
    }

    @RequestMapping("/index")
    public ModelAndView toIndex(HttpServletRequest request){
        ModelAndView mv = new ModelAndView();
        String path = request.getContextPath();
        String basePath = request.getScheme() + "://" + request.getServerName()
                + ":" + request.getServerPort() + path + "/";
        mv.addObject("basePath", basePath);
        mv.setViewName("/home/main");
        return mv;
    }

    @RequestMapping("/navigator")
    public ModelAndView navigator(HttpServletRequest request, HttpServletResponse response){
        String path = request.getContextPath();
        String basePath = request.getScheme() + "://" + request.getServerName()
                + ":" + request.getServerPort() + path + "/";

        String page = request.getParameter("page");
        //LoginUser loginUser = SystemUtil.getUserFromSession(request);
        Long userId = SystemUtil.getUserFromSession(request);
        if(isLogout(userId, page)){
            try{
                response.reset();
                response.setContentType("text/html;charset=utf-8");
                PrintWriter out = response.getWriter();
                out.println("<html>");
                out.println("<script>");
                out.println("window.open('"+basePath+"','_parent')");
                out.println("</script>");
                out.println("</html>");
            }catch(Exception e){
                System.err.println(e.getMessage());
            }
            return null;
        }else{
            ModelAndView mv = new ModelAndView();
            String id = request.getParameter("id");
            if (id != null && !"".equalsIgnoreCase(id)) {
                mv.addObject("id", id);
            }
            mv.addObject("basePath", basePath);
            mv.setViewName(page);
            //mv.setViewName("/home/main");
            return mv;
        }
    }

    private boolean isLogout(Long userId, String page){
        if (PAGE_LIST.contains(page)) {
            return false;
        } else if (userId == null || "logout".equalsIgnoreCase(page)){
            return true;
        } else {
            return true;
        }
    }

    @RequestMapping(value = "/navigatorWithParam",method = {RequestMethod.GET})
    public ModelAndView navigatorWithParam(HttpServletRequest request,HttpServletResponse response){
        String path = request.getContextPath();
        String basePath = request.getScheme() + "://" + request.getServerName()
                + ":" + request.getServerPort() + path + "/";

        String page = request.getParameter("page");
        String param = request.getParameter("param");

        ModelAndView mv = new ModelAndView();
        Long loginUser = SystemUtil.getUserFromSession(request);
        if(loginUser == null){
            try{
                response.reset();
                response.setContentType("text/html;charset=utf-8");
                PrintWriter out = response.getWriter();
                out.println("<html>");
                out.println("<script>");
                out.println("window.open('"+basePath+"','_parent')");
                out.println("</script>");
                out.println("</html>");
            }catch(Exception e){
                System.out.println(e.getMessage());
            }

            return null;
        }else{
            mv.addObject("currentUser", loginUser);
            mv.addObject("basePath", basePath);
            mv.addObject("param", param);
            mv.setViewName("/useMedicine/useMedicine");
            return mv;
        }

    }

    /*
    @RequestMapping("/interfaceContent")
    public String interfaceContent(HttpServletRequest request){
        String moduleId = request.getParameter("moduleId");
        request.setAttribute("moduleId" , moduleId);
        return  "interfaceContent";
    }
    */



    @RequestMapping("/flowNodeDetail")
    public String requestPayload(HttpServletRequest request){
        String nodeId = request.getParameter("nodeId");
        request.setAttribute("nodeId" , nodeId);
        return  "flowNodeDetail";
    }


}
